//-----------------------------------------------------------------------
// <copyright file="TaskPresenter.cs" company="Simnova LLC">
//     Copyright (c) Simnova LLC. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Simnova.Collaborate.Presenter
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    public class TaskPresenter
    {
        private readonly View.ITask taskView;
        private AppServer.Task task;

        public TaskPresenter(View.ITask taskView)
        {
            this.taskView = taskView;
        }

        public void LoadTasksByProject(int projectId)
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            this.taskView.TaskList = collaborateClient.GetTasksByProject(projectId);
            collaborateClient.Close();
        }

        public void LoadTasksByUser(int userId)
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            this.taskView.TaskList = collaborateClient.GetTasksByUser(userId);
            collaborateClient.Close();
        }

        public void LoadTask(int taskId)
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            this.task = collaborateClient.GetTask(taskId);
            this.taskView.TaskId = this.task.TaskId;
            this.taskView.UserId = this.task.UserId;
            this.taskView.ProjectId = this.task.ProjectId;
            this.taskView.Name = this.task.Name;
            collaborateClient.Close();
        }

        public void Save()
        {
            // Ensure our project object is in sync with the view.
            this.task.TaskId = this.taskView.TaskId;
            this.task.UserId = this.taskView.UserId;
            this.task.ProjectId = this.taskView.ProjectId;
            this.task.Name = this.taskView.Name;

            // Let the application server take care of saving the object. 
            // (this will happen over the network)
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            collaborateClient.SaveTask(this.task);
            collaborateClient.Close();
        }
    }
}
